//
//  FLMWebViewController.m
//  FLMSuperS
//
//  Created by xiaoyang on 16/9/19.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "FLMWebViewController.h"

#import <WebKit/WebKit.h>

@interface FLMWebViewController ()

@property (weak, nonatomic) IBOutlet UIProgressView *progressView;

@property (nonatomic, weak) WKWebView *wkView;
@end

@implementation FLMWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加wkWebView
    WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
    _wkView = webView;
    [self.view insertSubview:webView atIndex:0];
    
    //加载网页
    NSURLRequest *request = [NSURLRequest requestWithURL:_url];
    
//    NSLog(@"%@", _url);

    [webView loadRequest:request];
    
    //监听webView的estimedProgress
    [webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    
}

#pragma mark --------------------
#pragma mark 只要监听属性有新的值就会调用这个方法
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    _progressView.progress = _wkView.estimatedProgress;
    _progressView.hidden = _progressView.progress >= 1;
    
}

#pragma mark --------------------
#pragma mark KVO移除观察者
- (void)dealloc
{
    [self.wkView removeObserver:self forKeyPath:@"estimatedProgress"];
}

@end
